﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using CANO.Data.DAOs;

namespace CANO.WebSite.admin
{
	public partial class UsersAdmin : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            if ((Int32)Session["CurrentRolID"] > 1)
				Response.Redirect("/Index.aspx");

			btnNuevo.ServerClick += new EventHandler(btnNuevo_ServerClick);
			btnEditar.ServerClick += new EventHandler(btnEditar_ServerClick);
			btnVolver.ServerClick += new EventHandler(btnVolver_ServerClick);
			btnEliminar.ServerClick += new EventHandler(btnEliminar_ServerClick);
		}

		void btnEliminar_ServerClick(object sender, EventArgs e)
		{
			try
			{
				RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
				UsuarioDAO.Delete(Convert.ToInt32(sm.SelectedRows[0].RecordID));
				Response.Redirect("/admin/UsersAdmin.aspx");
				//Response.Redirect("/admin/User.aspx?Mode=Edit&UserID=" + sm.SelectedRows[0].RecordID);
			}
			catch (Exception)
			{
				pnlError.Visible = true;
				lblError.Text = "Seleccione un usuario del listado.";
			}
		}

		void btnVolver_ServerClick(object sender, EventArgs e)
		{
			Response.Redirect("/admin/Admin.aspx");
		}

		void btnEditar_ServerClick(object sender, EventArgs e)
		{
			try
			{
				RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
				Response.Redirect("/admin/User.aspx?Mode=Edit&UserID=" + sm.SelectedRows[0].RecordID);
			}
			catch (Exception)
			{
				pnlError.Visible = true;
				lblError.Text = "Seleccione un usuario del listado.";
			}
			
		}

		void btnNuevo_ServerClick(object sender, EventArgs e)
		{
			Response.Redirect("/admin/User.aspx?Mode=New");
		}
	}
}